package lucene.service.impl;

import com.google.common.base.Function;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import lucene.dao.IndexingGoodsDao;
import lucene.entity.Cat;
import lucene.entity.IndexedGoods;
import lucene.entity.Prop;
import lucene.entity.PropVal;
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.beans.Field;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.vacoor.nothing.common.util.StringUtils;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 */
public class NoahsIndexingServiceImpl extends GeneralIndexingServiceImpl {

    @Override
    protected Object toIndexedBean(IndexedGoods entity) {
        return GoodsIndexedBean.wrap(entity);
    }

    /**
     * 临时解决方案，不要骂我
     *
     * @author vacoor
     */
    private static class GoodsIndexedBean {
        @Field
        private String id;
        @Field
        private String title;
        @Field
        private String image;
        @Field
        private String price;
        @Field
        private String marketPrice;
        @Field
        private Date putawayDate;
        @Field
        private Long sales;
        @Field("brand")
        private String brandId;
        @Field("brandName")
        private String brandName;
        @Field("cat")
        private String catId;
        @Field("catName")
        private String catName;
        @Field("pcat")
        private String pcatId;
        @Field("pcatName")
        private String pcatName;
        @Field("props")
        private List<String> propKeys = Lists.newArrayList();
        @Field("prop_*")
        private Map<String, String> propAndValueMap = Maps.newHashMap();
        @Field("prop_v_*")
        private Map<String, String> propAndValueNames = Maps.newHashMap();

        public static GoodsIndexedBean wrap(IndexedGoods goods) {
            return new GoodsIndexedBean(goods);
        }

        private GoodsIndexedBean(IndexedGoods goods) {
            if (null == goods) {
                throw new IllegalArgumentException("indexed goods must be not null");
            }
            id = goods.getId();
            title = goods.getTitle();
            title = StringUtils.hasText(title) ? title : goods.getProductTitle();
            List<String> images = goods.getImages();
            if (null != images && 0 < images.size()) {
                image = images.get(0);
            }
            price = goods.getPrice();
            marketPrice = goods.getMarketPrice();
            putawayDate = goods.getPutawayDate();
            sales = goods.getSales();
            brandId = goods.getBrandId();
            brandName = goods.getBrandName();
            List<Cat> cats = goods.getCats();
            if (!CollectionUtils.isEmpty(cats)) {
                for (Cat cat : cats) {
                    catId = cat.getId();
                    catName = cat.getName();
                    Cat p = cat.getParent();
                    if (null != p) {
                        pcatId = p.getId();
                        pcatName = p.getName();
                    }
                }
            }
            List<PropVal> values = goods.getPropVals();
            if (!CollectionUtils.isEmpty(values)) {
                for (PropVal value : values) {
                    Prop prop = value.getProp();
                    if (null == prop) {
                        continue;
                    }

                    String sid = prop.getId();
                    String sname = prop.getName();
                    String vid = value.getId();
                    String firstValue = value.getFirstValue();
                    String secondValue = value.getSecondValue();

                    if (StringUtils.hasText(secondValue)) {
                        firstValue += "-" + secondValue;
                    }

                    propKeys.add(sid);
                    propAndValueMap.put("prop_" + sid, vid);
                    propAndValueNames.put("prop_v_" + vid, sname + "_" + firstValue);
                }
            }
        }
    }

}
